package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TdDish;
import com.ruoyi.system.service.ITdDishService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * 楼盘管理Controller
 * 
 * @author lpz
 * @date 2025-01-02
 */
@RestController
@RequestMapping("/merchant/dish")
public class TdDishController extends BaseController
{
    @Autowired
    private ITdDishService tdDishService;

    /**
     * 查询楼盘管理列表
     */
    @PreAuthorize("@ss.hasPermi('merchant:dish:list')")
    @GetMapping("/list")
    public TableDataInfo list(TdDish tdDish)
    {
        startPage();
        List<TdDish> list = tdDishService.selectTdDishList(tdDish);
        return getDataTable(list);
    }

    /**
     * 导出楼盘管理列表
     */
    @PreAuthorize("@ss.hasPermi('merchant:dish:export')")
    @Log(title = "楼盘管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TdDish tdDish)
    {
        List<TdDish> list = tdDishService.selectTdDishList(tdDish);

        // 处理 name 和 image 字段
        list.forEach(dish -> {
            dish.setName(parseJsonArray(dish.getName()));
            dish.setImage(parseJsonArray(dish.getImage()));
        });

        ExcelUtil<TdDish> util = new ExcelUtil<TdDish>(TdDish.class);
        util.exportExcel(response, list, "楼盘管理数据");
    }

    // 辅助方法：解析 JSON 字符串为可读格式
    private String parseJsonArray(String jsonString) {
        if (jsonString == null || jsonString.isEmpty()) {
            return "";
        }
        try {
            JSONArray jsonArray = new JSONArray(jsonString);
            StringBuilder result = new StringBuilder();
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                if (jsonObject.has("ne")) {
                    result.append(jsonObject.getString("ne"));
                    if (i < jsonArray.length() - 1) {
                        result.append(", ");
                    }
                }
            }
            return result.toString();
        } catch (JSONException e) {
            e.printStackTrace();
            return jsonString; // 如果解析失败，返回原始字符串
        }
    }

    /**
     * 获取楼盘管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('merchant:dish:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tdDishService.selectTdDishById(id));
    }

    /**
     * 新增楼盘管理
     */
    @PreAuthorize("@ss.hasPermi('merchant:dish:add')")
    @Log(title = "楼盘管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TdDish tdDish)
    {
        return toAjax(tdDishService.insertTdDish(tdDish));
    }

    /**
     * 修改楼盘管理
     */
    @PreAuthorize("@ss.hasPermi('merchant:dish:edit')")
    @Log(title = "楼盘管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TdDish tdDish)
    {
        return toAjax(tdDishService.updateTdDish(tdDish));
    }

    /**
     * 删除楼盘管理
     */
    @PreAuthorize("@ss.hasPermi('merchant:dish:remove')")
    @Log(title = "楼盘管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tdDishService.deleteTdDishByIds(ids));
    }
}
